package com.bestcem.xm.award.controller.convert;

import cn.hutool.core.lang.Validator;
import com.bestcem.xm.award.constant.RedPacketConstant;
import com.bestcem.xm.award.controller.vo.FileInfoVO;
import com.bestcem.xm.award.controller.vo.redpacket.*;
import com.bestcem.xm.award.service.dto.*;
import com.bestcem.xm.common.core.uitls.DateUtil;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 微信红包奖励,DTO转VO
 *
 * @author jie.yao <jie.yao@idiaoyan.com>
 * @version v3.0
 * @date 2021/8/17 15:19
 */
public class RedPacketConvert {

    /**
     * 获取投放数据统计DeliverDetailDTO转DeliverDetailVO
     *
     * @param deliverDetailList 投放数据统计
     * @return ListDeliverDetailVO
     * @author jie.yao <jie.yao@idiaoyan.com>
     * @date 2021/8/17 15:33
     */
    public static ListDeliverDetailVO deliverDetailConversion(List<DeliverDetailDTO> deliverDetailList) {
        List<DeliverDetailVO> deliverDetailVOList = deliverDetailList.stream().map(dto -> {
            DeliverDetailVO vo = new DeliverDetailVO();
            BeanUtils.copyProperties(dto, vo);
            return vo;
        }).collect(Collectors.toList());
        ListDeliverDetailVO listDeliverDetailVO = new ListDeliverDetailVO();
        listDeliverDetailVO.setList(deliverDetailVOList);
        return listDeliverDetailVO;
    }

    /**
     * 查看红包详情页面中的中奖名单WinDetailDTO转WinDetailVO
     *
     * @param winDetailDTO 红包详情页面中的中奖名单
     * @param page         页数
     * @return WinDetailVO
     * @author jie.yao <jie.yao@idiaoyan.com>
     * @date 2021/8/17 16:48
     */
    public static WinDetailVO winDetailConversion(WinDetailDTO winDetailDTO, Integer page) {
        WinDetailVO winDetailVO = new WinDetailVO();
        // 当前中奖数
        winDetailVO.setTotal(winDetailDTO.getTotal());
        // 列表单行数据
        List<WinDetailRowVO> winDetailRowVOList = winDetailDTO.getList().stream().map(dto -> {
            WinDetailRowVO vo = new WinDetailRowVO();
            BeanUtils.copyProperties(dto, vo);
            if (Validator.isNotNull(dto.getSendTime())) {
                vo.setSendTime(DateUtil.dateFormatToWeb(dto.getSendTime()));
            }
            return vo;
        }).collect(Collectors.toList());
        winDetailVO.setList(winDetailRowVOList);
        // 页数
        winDetailVO.setPage(page);
        return winDetailVO;
    }

    /**
     * 导出红包中奖明细FileInfoDTO转FileInfoVO
     *
     * @param fileInfoDTO 导出文件信息
     * @return FileInfoVO
     * @author jie.yao <jie.yao@idiaoyan.com>
     * @date 2021/8/17 17:51
     */
    public static FileInfoVO winExportFileInfoConversion(FileInfoDTO fileInfoDTO) {
        FileInfoVO fileInfoVO = new FileInfoVO();
        BeanUtils.copyProperties(fileInfoDTO, fileInfoVO);
        return fileInfoVO;
    }

    /**
     * 二维码后面的红包抽奖入口页，生成requestId DTO 转 VO
     *
     * @param redPacketEntryDTO DTO
     * @return VO
     */
    public static GetRedPacketDrawOuterVO getRedPacketDrawConversion(RedPacketEntryDTO redPacketEntryDTO, String requestId) {
        GetRedPacketDrawVO getRedPacketDrawVO = new GetRedPacketDrawVO();
        BeanUtils.copyProperties(redPacketEntryDTO, getRedPacketDrawVO);
        getRedPacketDrawVO.setRequestId(requestId);
        GetRedPacketDrawOuterVO getRedPacketDrawOuterVO = new GetRedPacketDrawOuterVO();
        getRedPacketDrawOuterVO.setInfo(getRedPacketDrawVO);
        return getRedPacketDrawOuterVO;
    }

    /**
     * 获取红包总览 DTO 转 VO
     *
     * @param redPacketStatsDTO DTO
     * @return VO
     */
    public static GetRedPacketStatsOuterVO getRedPacketStatsConversion(RedPacketStatsDTO redPacketStatsDTO) {
        GetRedPacketStatsVO getRedPacketStatsVO = new GetRedPacketStatsVO();
        getRedPacketStatsVO.setSentCount(redPacketStatsDTO.getSentCount());
        getRedPacketStatsVO.setSentAmount(convertAmountToInt(redPacketStatsDTO.getSentAmount()));
        getRedPacketStatsVO.setWaitCount(redPacketStatsDTO.getWaitCount());
        getRedPacketStatsVO.setWaitAmount(convertAmountToInt(redPacketStatsDTO.getWaitAmount()));
        getRedPacketStatsVO.setTotalCount(redPacketStatsDTO.getTotalCount());
        getRedPacketStatsVO.setTotalAmount(convertAmountToInt(redPacketStatsDTO.getTotalAmount()));
        return new GetRedPacketStatsOuterVO(getRedPacketStatsVO);
    }

    /**
     * 人民币单位转换 元->分
     *
     * @param bigDecimal n元
     * @return n分
     */
    private static int convertAmountToInt(BigDecimal bigDecimal) {
        if (bigDecimal == null) {
            return 0;
        }
        return bigDecimal.multiply(new BigDecimal(RedPacketConstant.INT100)).intValue();
    }
}
